/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thoughtbot.expandablerecyclerview.sample.multitype;

import com.thoughtbot.expandablecheckrecyclerview.expand.ExpandFatherItem;
import com.thoughtbot.expandablecheckrecyclerview.multitype.MultiTypeChildItem;
import com.thoughtbot.expandablecheckrecyclerview.multitype.MultiTypeContainer;
import com.thoughtbot.expandablerecyclerview.sample.ResourceTable;
import com.thoughtbot.expandablerecyclerview.sample.Vo.ChildData;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MultiTypeAbility
 *
 * @since 2021-07-23
 */
public class MultiTypeAbility extends Ability {
    private MultiTypeContainer listContainer;
    private Button backButton;

    /**
     * onStart 生命周期方法
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_multitype);

        // 设置状态栏颜色
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(new Color(0xFFD32E2E).getValue());
        initView();
    }

    private void initView() {
        listContainer = (MultiTypeContainer)findComponentById(ResourceTable.Id_multiType_container);
        listContainer.setFatherData(getData());
        listContainer.setChildData(getChildMap());
        backButton = (Button)findComponentById(ResourceTable.Id_backButton);
        backButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
    }

    private List<ExpandFatherItem> getData() {
        ArrayList<ExpandFatherItem> data = new ArrayList<>();
        VectorElement vectorElementLabel = new VectorElement(getContext(),ResourceTable.Graphic_ic_arrow_down);
        VectorElement vectorElementRock = new VectorElement(getContext(),ResourceTable.Graphic_ic_electric_guitar);
        VectorElement vectorElementJazz = new VectorElement(getContext(),ResourceTable.Graphic_ic_saxaphone);
        VectorElement vectorElementClassic = new VectorElement(getContext(),ResourceTable.Graphic_ic_violin);
        VectorElement vectorElementSalsa = new VectorElement(getContext(),ResourceTable.Graphic_ic_maracas);
        VectorElement vectorElementBluegrass = new VectorElement(getContext(),ResourceTable.Graphic_ic_banjo);
        data.add(new ExpandFatherItem("0",vectorElementRock,"Rock",vectorElementLabel));
        data.add(new ExpandFatherItem("1",vectorElementJazz,"Jazz",vectorElementLabel));
        data.add(new ExpandFatherItem("2",vectorElementClassic,"Classic",vectorElementLabel));
        data.add(new ExpandFatherItem("3",vectorElementSalsa,"Salsa",vectorElementLabel));
        data.add(new ExpandFatherItem("4",vectorElementBluegrass,"Bluegrass",vectorElementLabel));
        return data;
    }

    /**
     * 填充子组件的数据
     *
     * @param name name
     * @param fatherGroup fatherGroup
     * @return List
     */
    private List<MultiTypeChildItem> getChildData(String[] name, String fatherGroup) {
        ArrayList<MultiTypeChildItem> data = new ArrayList<>();
        VectorElement vectorElement = new VectorElement(getContext(),ResourceTable.Graphic_ic_star);
        for (int i = 0; i < name.length; i++) {
            MultiTypeChildItem item = new MultiTypeChildItem(name[i],false,vectorElement);
            switch (fatherGroup) {
                case "0":
                    if (i == 0 || i == 3) {
                        item = new MultiTypeChildItem(name[i],true,vectorElement);
                    }
                    break;
                case "1":
                    if (i == 0 || i == 1) {
                        item = new MultiTypeChildItem(name[i],true,vectorElement);
                    }
                    break;
                case "2":
                    if (i == 1) {
                        item = new MultiTypeChildItem(name[i],true,vectorElement);
                    }
                    break;
                case "3":
                    if (i == 0) {
                        item = new MultiTypeChildItem(name[i],true,vectorElement);
                    }
                    break;
                case "4":
                    if (i == 2) {
                        item = new MultiTypeChildItem(name[i],true,vectorElement);
                    }
                    break;
                default:
                    item = new MultiTypeChildItem(name[i],false,vectorElement);
            }
            data.add(item);
        }
        return data;
    }

    /**
     * 子组件数据绑定到指定的父组件上
     *
     * @return Map
     */
    private Map<String, List<MultiTypeChildItem>> getChildMap() {
        Map<String, List<MultiTypeChildItem>> childMap = new HashMap<>();
        childMap.put("0",getChildData(ChildData.ROCK_CHILD,"0"));
        childMap.put("1",getChildData(ChildData.JAZZ_CHILD,"1"));
        childMap.put("2",getChildData(ChildData.CLASSIC_CHILD,"2"));
        childMap.put("3",getChildData(ChildData.SALSA_CHILD,"3"));
        childMap.put("4",getChildData(ChildData.BLUEGRASS_CHILD,"4"));
        return childMap;
    }
}
